<?php
class ModLogin
{
	public function index()
	{
			viewLayer::$var->loggedIn = sessionLayer::getAccount();
			viewLayer::render('login');
	}
	
	private function toPassword( $str )
	{
		$passCryption = configParser::getData('passwordtype');
		
		switch( strtolower($passCryption) )
		{
			case "plain":
				return $str;
			case "sha1":
				return sha1($str);
			case "md5":
				return md5($str);
		}
	}
	
	public function verify()
	{
		if( $_POST['acc'] && $_POST['password'] )
		{
			$a = new OTS_Account;
			if( is_numeric( $_POST['acc'] ) )
			{
				$a->load( $_POST['acc'] );
			}
			else
			{
				$a->find( $_POST['acc'] );
			}
			
			if( $a->isLoaded() && $a->getPassword() == $this->toPassword($_POST['password']) )
			{
				sessionLayer::setAccount( $a->getId() );
				sessionLayer::setPassword( $a->getPassword() );
				viewLayer::render(NULL,'Logged sucessfull, please go to <a href="?m=account">account</a> panel.');
			}
			else
			{
				viewLayer::render(NULL,'You have inserted incorrect data. Please <a href="?m=login">login</a> again.');
			}
		}
		else
		{
			viewLayer::render(NULL,'You haven\'t complete a form. Please <a href="?m=login">login</a> again.');
		}
	}
	public function out()
	{
		sessionLayer::destroy();
		viewLayer::render(NULL,'You have logged out succesfull.');
	}
}
?>